<?php

namespace nimo\common\models;

use Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class BaseModel extends Eloquent {

    // protected static $softDeletesEnabled = false|true;
    use SoftDeletingTrait, MultiKeyTrait { MultiKeyTrait::bootSoftDeletingTrait insteadof SoftDeletingTrait;}

    // protected $forceDeleting = false;
    protected function performDeleteOnModel() {
        $trashable = static::softDeletesEnabled() && !$this->forceDeleting;
        $keys = $this->getKey();
        if (!is_array($keys)) {
            $keys = array($this->getKeyName() => $keys );
        };
        // forever delete
        $query = $this->newQuery();
        foreach ( $keys as $key => $value ) {
            $query = $query->where($key, '=', $value);
        }
        if ($trashable) {
            // soft delete
            $this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
            return $query->update(array($this->getDeletedAtColumn() => $this->fromDateTime($time)));
        }
        if ($this->forceDeleting) {
            return $query->forceDelete();
        }
        return $query->delete();
    }
}

